package cn.i96688.scanclient.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;







import com.alibaba.fastjson.JSONObject;
import cn.i96688.scanclient.service.MerchantService;
import cn.i96688.scanclient.service.impl.MerchantServiceImpl;
import cn.i96688.scanclient.utils.ConfParams;

/**
 * 登录与参数设置
 * @author xys
 *
 */
@SuppressWarnings("serial")
public class LoginForm  extends SuperForm{
	private BorderLayout mainLayout=new BorderLayout();
	private ParamPane paramPane;
	private MerchantService paramService=new MerchantServiceImpl();
	
	private SaveListener saveListener=null;
	
	public LoginForm(){
		this(null);
	}
	//登录名，密码
	public LoginForm(SaveListener l){
		super();
		saveListener=l;
		setSize(300,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("参数设置");
		setIconImage(MainForm.icon);
		this.setLayout(mainLayout);
		add(new JLabel("  "),BorderLayout.WEST);
		add(new JLabel("  "),BorderLayout.EAST);
		JLabel title=new JLabel("\n登录系统",JLabel.CENTER);
		title.setAlignmentY(BOTTOM_ALIGNMENT);
		title.setPreferredSize(new Dimension(200,35));
		title.setFont(new Font("宋体",Font.BOLD,15));
		add(title, BorderLayout.NORTH);
		paramPane=new ParamPane();
		add(paramPane, BorderLayout.CENTER);
		
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	
	class ParamPane extends SuperPane{
		private GridBagLayout layout=null;
		private JLabel loginLab=new JLabel();
		private JTextField loginName=new JTextField();
		private JLabel passwordLab=new JLabel();
		private JPasswordField password=new JPasswordField();
		private JButton saveBtn;
		
		boolean checkField(){
			if(loginName.getText()==null || loginName.getText().trim().length()==0){
				JOptionPane.showConfirmDialog(this, "登录账户不能为空", "数据检验错误", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if(password.getPassword()==null || password.getPassword().length==0){
				JOptionPane.showConfirmDialog(this, "登录密码不能为空", "数据检验错误", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			return true;
		}
		
		JSONObject getParams(){
			JSONObject params=new JSONObject();
			params.put("username", loginName.getText());
			params.put("password", new String(password.getPassword()));
			params.put("deviceName", ConfParams.getValueByKey("deviceName"));
			return params;
		}
		
		ParamPane(){
			layout=new GridBagLayout();//创建网格组布局方式对象 
	        setLayout(layout); 
	        
	        
	        //账户
	        loginLab.setText("登录账户");
	        add(loginLab,new GBC(0,0));
	        add(loginName,new GBC(1,0).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(2));
	       
	        //密码
	        passwordLab.setText("登录密码");
	        add(passwordLab,new GBC(0,1));
	        add(password,new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(2));
	        
	        saveBtn=new JButton("登录并保存");
	        add(saveBtn,new GBC(1,2).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(2));
			saveBtn.addActionListener(this);
			
			 add(new JLabel(" "),new GBC(1,3).setFill(GBC.HORIZONTAL));
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(checkField()){
				String msg=paramService.login(getParams());
				if(msg.length()>0){
					JOptionPane.showMessageDialog(this, msg, "操作错误", JOptionPane.ERROR_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(this, "参数配置成功", "操作成功", JOptionPane.INFORMATION_MESSAGE);
					LoginForm.this.dispose();
					if(saveListener!=null)
						saveListener.save(true);
				}
			}
		}
	}
	
	public static interface SaveListener{
		void save(boolean status);
	}
}
